#include "precompiled.h"
#include "PhysicsCommon.h"
#include "Physics_Scenery.h"

//...................................................
#include "tbb/concurrent_hash_map.h"
using namespace tbb;
//...................................................

namespace RigidBodyGameObjectMap{


	// A map of rigidbody id's and their associated game objects.
	typedef concurrent_hash_map<hkUint32, Physics_Scenery*> rbidgameo;
	rbidgameo rb_go_map;
	typedef rbidgameo::accessor rbidgameo_accessor;
	typedef rbidgameo::const_accessor rbidgameo_constant_accessor;

	void RigidBodyGameObjectMap::Initialize(){}

	void RigidBodyGameObjectMap::add_pair_scenery( hkUint32 rbid,  Physics_Scenery* scenery ){
		rbidgameo_accessor a;
		rb_go_map.insert(a, rbid);
		a->second = scenery;
	}

	Physics_Scenery* RigidBodyGameObjectMap::get_scenery( hkUint32 rbid ){
		rbidgameo_constant_accessor ca;
		if( rb_go_map.find( ca, rbid ) )
			return ca->second;
		return 0;
	}

};

namespace RigidBodyUniqueID{

	hkUint32 curID = 0;
	hkUint32 GenerateUniqueID(){
		return curID++;
	}
};



//.....................................................................
// This class centers around the implementations of the two virtual methods in the base class, namely
// addOverlappingCollidable(...) and removeOverlappingCollidable(...). The code for these methods is given below.
// Note: This (very simple) demo works fine without implementing either of these functions. Deriving an entirely
// new hkpAabbPhantom class is not strictly necessary either...

hkMovingRBCollectorPhantom::hkMovingRBCollectorPhantom( const hkAabb& aabb, hkUint32 collisionFilterInfo  )
	: hkpAabbPhantom(aabb, collisionFilterInfo)
{
}


void hkMovingRBCollectorPhantom::addOverlappingCollidable( hkpCollidable* c )
{
	hkpRigidBody* rb = hkpGetRigidBody(c);

	// Ignore other phantoms and fixed rigid bodies.
	if ( (rb != HK_NULL) && !rb->isFixed() )
	{
		hkpAabbPhantom::addOverlappingCollidable( c );
	} 
}

void hkMovingRBCollectorPhantom::removeOverlappingCollidable( hkpCollidable* c )
{
	hkpRigidBody* rb = hkpGetRigidBody(c);

	// hkpAabbPhantom::removeOverlappingCollidable() protects its m_overlappingCollidables member,
	// regardless of whether collidables are present in it or not. However this check needs to be
	// symmetric with the one above, if we want symmetry in callbacks fired within 
	// hkpAabbPhantom::removeOverlappingCollidable() and hkpAabbPhantom::addOverlappingCollidable().
	if ( (rb != HK_NULL) && !rb->isFixed() )
	{
		hkpAabbPhantom::removeOverlappingCollidable( c );
	}
}

void hkMovingRBCollectorPhantom::set_particle( Particle* p ){
	ref_particle=p;
}
Particle* hkMovingRBCollectorPhantom::get_particle(){
	return ref_particle;
}
//.....................................................................
